package com.dragon.app.commons.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dragon.app.commons.constant.ErrorCode;
import com.dragon.app.commons.dto.BankcardSaveDTO;
import com.dragon.app.commons.utils.UserUtils;
import com.dragon.app.marvel2.NewMarvelCoreService;
import com.dragon.app.ykd.constant.ApplyNode;
import com.dragon.app.ykd.dto.AcLoanApplyNode;
import com.dragon.app.ykd.dto.BankCardInfosDTO;
import com.dragon.app.ykd.dto.BankCardLoanDTO;
import com.dragon.app.ykd.service.AcLoanApplyNodeService;
import com.dragon.common.constant.LoanStatusConstant;
import com.dragon.loan.entity.AcLoanApplyInfo;
import com.dragon.loan.service.LoanInfoIDragonService;
import com.fintech.common.entity.ResultData;
import com.fintech.common.enums.ReturnCode;
import com.fintech.common.vo.FintechResult;
import com.fintech.user.entity.RealNameAuthentication;
import com.fintech.user.entity.User;
import com.fintech.user.service.RealNameAuthenticationService;
import com.marvels.approve.loan.api.LoanApplyProvideApi;
import com.marvels.common.dto.pay.MarvelsPayResultDTO;
import com.marvels.common.dto.pay.McfePaymentUserBindCard;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.ServerProperties;
import com.marvels.financial.loan.dto.McfeSustainBankListDTO;
import com.marvels.financial.loan.service.BankLimitRelevantApi;
import com.marvels.financial.pay.service.PayService;

/**
 * 银行或者关于银行卡
 * @author ljs
 *
 */
@Service("bankAppService")
public class BankServiceImpl implements BankService{
	@Autowired
	private LoanInfoIDragonService loanInfoIDragonService;
	@Autowired
	private RealNameAuthenticationService realNameAuthenticationService;
	@Autowired
	private NewMarvelCoreService newMarvelCoreService;
	@Autowired
	private AcLoanApplyNodeService acLoanApplyNodeService;
	@Autowired
	private LoanApplyProvideApi loanApplyProvideApi;
	@Autowired
	private PayService payService;
	@Autowired
	private BankLimitRelevantApi bankLimitRelevantApi;
	
	@Value("${config.properties.path}")
	private  String configPropertiesPath;
	
	/**
	 * 根据用户Id按照类型获取银行卡的信息<br>
	 * 0-放款卡,1-还款卡,2-信用卡<br>
	 * 
	 * @param shortUrl
	 * @param response
	 * @return
	 */
	@Override
	public FintechResult getBankCardList(String accessToken, String idFintechUmUser,
			BankCardInfosDTO bankCardInfosDTO) {
		List<Map<String, Object>> listAcUserBankByUserId = null;
		List<Map<String, Object>> returnList = new ArrayList<>();
		try {
			//1 获取用户信息以及实名认证信息
			User user = UserUtils.getUser(accessToken, idFintechUmUser);
			if (user == null) {
				return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
			}
			ResultData<Boolean> selectByUserIdResponse = realNameAuthenticationService.selectByUserId(user.getId());
			if (!selectByUserIdResponse.getData()) {
				return FintechResult.getResult(ErrorCode.NO_VERIFY, "", null);
			}
			RealNameAuthentication realNameAuthentication = realNameAuthenticationService.queryByUserId(user.getId());
			
			/*Map<String, String> reqMap = new HashMap<>();
			reqMap.put("idno", realNameAuthentication.getIdentityCardNo());
			FintechResult jsonResult = newMarvelCoreService.getForObjectPost(MarvelURLConstant.FINANCIAL_PAY_API_QUERYBINDEDLIST, reqMap);
			*/
			String idno = realNameAuthentication.getIdentityCardNo();
			String paymentChannel  = ServerProperties.getValue(configPropertiesPath, "common.properties", "payment_channel");
			MarvelsPayResultDTO data = payService.queryBindedSuccessList(idno, paymentChannel);
			FintechResult jsonResult = new FintechResult();
			jsonResult.setData(data.getData());
			jsonResult.setErrorCode(data.getCode());
			jsonResult.setMessage(data.getMessage());
			
			MarvelsLogUtil.getInstance().info("========getBankCardList:" + JSON.toJSONString(jsonResult));
			if (!ErrorCode.SUCCESS.equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "获取卡列表失败");
			}
			List<McfePaymentUserBindCard> rtnMap =  (List<McfePaymentUserBindCard>) jsonResult.getData();
			for (McfePaymentUserBindCard map : rtnMap) {
				Map<String, Object> bankMap = new HashMap<>();
				bankMap.put("id", map.getId());
				bankMap.put("bankCode", map.getBankCode());
				bankMap.put("bankName", map.getBankName());
				bankMap.put("mobileNum", map.getPhome());
				bankMap.put("bankCardNo", map.getBankCardNo());
				bankMap.put("defaultCard", map.getDefaultCard());
				bankMap.put("bankCardType", "借记卡");
				returnList.add(bankMap);
			}
			return FintechResult.getResult(ErrorCode.SUCCESS, "操作成功", returnList);
			
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("=====bankCard/infos=====", e);
			return FintechResult.getResult(ErrorCode.FAIL, "系统异常", null);
		}
	}
	
	
	/**
	 * 根据用户Id按照类型获取绑定银行卡信息<br>
	 * 0-放款卡,1-还款卡,2-信用卡<br>
	 * 
	 * @param shortUrl
	 * @param response
	 * @return
	 */
	@Override
	public FintechResult getBankBindList(String accessToken, String idFintechUmUser,
			BankCardInfosDTO bankCardInfosDTO) {
		List<Map<String, Object>> listAcUserBankByUserId = null;
		try {
			//1 获取用户信息以及实名认证信息
			User user = UserUtils.getUser(accessToken, idFintechUmUser);
			if (user == null) {
				return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
			}
			ResultData<Boolean> selectByUserIdResponse = realNameAuthenticationService.selectByUserId(user.getId());
			if (!selectByUserIdResponse.getData()) {
				return FintechResult.getResult(ErrorCode.NO_VERIFY, "", null);
			}
			RealNameAuthentication realNameAuthentication = realNameAuthenticationService.queryByUserId(user.getId());
			
			/*Map<String, String> reqMap = new HashMap<>();
			reqMap.put("idno", realNameAuthentication.getIdentityCardNo());
			FintechResult jsonResult = newMarvelCoreService.getForObjectPost(MarvelURLConstant.FINANCIAL_PAY_API_BANKBINDLIST, reqMap);*/
			
			Map<String, Object> reqMap = new HashMap<String, Object>();
			reqMap.put("idno", realNameAuthentication.getIdentityCardNo());
			MarvelsPayResultDTO data = payService.bankBindList(reqMap);
			FintechResult jsonResult = new FintechResult();
			jsonResult.setData(data.getData());
			jsonResult.setErrorCode(data.getCode());
			jsonResult.setMessage(data.getMessage());
			
			
			MarvelsLogUtil.getInstance().info("========getBankCardList:" + JSON.toJSONString(jsonResult));
			if (!ErrorCode.SUCCESS.equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "获取卡列表失败");
			}
			Map<String, Object> rtnMap =  (Map<String, Object>) jsonResult.getData();
			Map<String, Object> bankMap = new HashMap<>();
			bankMap.put("isBind", rtnMap.get("isBind"));
			
			McfePaymentUserBindCard bindCard =  (McfePaymentUserBindCard) rtnMap.get("bindCard");
			Map<String, Object> map =new HashMap<String, Object>();
			map.put("id", bindCard.getId());
			map.put("bankCode", bindCard.getBankCode());
			map.put("bankName", bindCard.getBankName());
			map.put("mobileNum", bindCard.getPhome());
			map.put("bankCardNo", bindCard.getBankCardNo());
			map.put("bankCardType", "借记卡");
			bankMap.put("bindCard", map);
			return FintechResult.getResult(ErrorCode.SUCCESS, "操作成功", bankMap);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("=====bankCard/infos=====", e);
			return FintechResult.getResult(ErrorCode.FAIL, "系统异常", null);
		}
	}
	
	/**
	 * 接收用户填写的的宝付发送给客户的验证
	 * 去宝付那边获取这个卡跟这个商户号对应的协议号
	 * @param bankcardSaveDTO 
	 * @param accessToken
	 * @param idFintechUmUser
	 * @return
	 * @author shan
	 */
	@Override
	public FintechResult saveBankCardNew(BankcardSaveDTO bankcardSaveDTO,
			 String accessToken,
			 String idFintechUmUser) {
		UUID randomUUID = UUID.randomUUID();
		String uuid = randomUUID.toString();

		MarvelsLogUtil.getInstance().info("-----------bankCard/save------s--------" + uuid + "----" + bankcardSaveDTO.toString());
		try {
			/*Map<String, String> reqMap = new HashMap<>();
			reqMap.put("requestNo", bankcardSaveDTO.getRequestNo());
			reqMap.put("validateCode", bankcardSaveDTO.getValidCode());
			FintechResult jsonResult = newMarvelCoreService.getForObjectPost(MarvelURLConstant.FINANCIAL_PAY_API_BINDCARD, reqMap);*/
			
			String paymentChannel  = ServerProperties.getValue(configPropertiesPath, "common.properties", "payment_channel");
			MarvelsPayResultDTO data = payService.bindCard(bankcardSaveDTO.getRequestNo(), bankcardSaveDTO.getValidCode(), paymentChannel);
			FintechResult jsonResult = new FintechResult();
			jsonResult.setErrorCode(data.getCode());
			jsonResult.setData(data.getData());
			jsonResult.setMessage(data.getMessage());
			
			
			MarvelsLogUtil.getInstance().info("========saveBankCardNew:" + JSON.toJSONString(jsonResult));
			// 已经绑定的卡不能再绑定
			if ("-10000001".equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "您的卡[" + bankcardSaveDTO.getBankCardNum() + "]已绑定，不能重复绑定");
			}
			if (!ErrorCode.SUCCESS.equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "绑卡异常");
			}
			String status = (String) jsonResult.getData();
			// 成功状态
			if ("BIND_SUCCESS".equals(status)) {
				return FintechResult.getResult(ErrorCode.SUCCESS, "绑卡成功", null);
			}
			return FintechResult.getFailResult(ErrorCode.FAIL, "绑卡失败");
			
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("-----------bankCard/save------error-------" + uuid + "-------" + bankcardSaveDTO.toString(),
						e);
			return FintechResult.getResult(ErrorCode.FAIL, "系统异常", null);
		}
	}
	
	/**
	 * 设置放款卡 还款卡 type 1 还款卡 2 放款卡 id 放款卡的id
	 * 线上绑卡
	 * @param shortUrl
	 * @param response
	 * @return
	 */
	@Override
	public FintechResult bankCardLoan(String loanId, BankCardLoanDTO bankCardLoanDTO,
			String accessToken, String idFintechUmUser) {
		
		MarvelsLogUtil.getInstance().info("-----------bankCard/loan/{loanId}------s------------" + JSON.toJSONString(bankCardLoanDTO)+ "--loanId = " + loanId);
		AcLoanApplyInfo loan = new AcLoanApplyInfo();
		try {
			
			//1 检查
			User user = UserUtils.getUser(accessToken, idFintechUmUser);
			if (user == null) {
				return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
			}
			
			//1.1 是否存在进件信息
			AcLoanApplyInfo acLoanApplyInfo = loanInfoIDragonService.queryLoanBasicInfo(loanId);
			if(acLoanApplyInfo == null){
				return FintechResult.getResult(ErrorCode.EXCEPTION, "未查询到进件信息", null); 
			}
			
			//1.2 校验申请记录的贷款状态
			if (LoanStatusConstant.LOAN_STATUS_CR0.equals(acLoanApplyInfo.getStatus())) {
				return FintechResult.getResult(ErrorCode.LIMIT_CALCULATE_DEFEATED, "您暂时不符合我们公司的申请条件", null);
			} else if (LoanStatusConstant.LOAN_STATUS_K01.equals(acLoanApplyInfo.getStatus())) {
				return FintechResult.getResult(ErrorCode.EXCEPTION, "重复提交,请返回进度详情重新进入", null);
			}
			
			
			Long productType = acLoanApplyInfo.getProductType();
			
			//2 设置卡操作
			/*Map<String, Object> map = new HashMap<>();
			map.put("bindId", bankCardLoanDTO.getBankCardId());
			FintechResult jsonResult = newMarvelCoreService.getForObjectPost(MarvelURLConstant.FINANCIAL_PAY_API_QUERYBINDCARDINFOBYID, map);*/
			if(bankCardLoanDTO.getBankCardId() == null){
				return FintechResult.getFailResult(ReturnCode.fail.getErrorCode(), "绑定卡信息入参为空");
			}
			/*MarvelsPayResultDTO data = payService.queryBindCardInfoById(bankCardLoanDTO.getBankCardId().longValue());
			FintechResult jsonResult = new FintechResult();
			jsonResult.setMessage(data.getMessage());
			jsonResult.setErrorCode(data.getCode());
			jsonResult.setData(data.getData());
			
			
			MarvelsLogUtil.getInstance().info("========bankCardLoan:" + JSON.toJSONString(jsonResult));
			if (!ErrorCode.SUCCESS.equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "获取卡信息异常");
			}
			McfePaymentUserBindCard cardMap = (McfePaymentUserBindCard) jsonResult.getData();
			String bankName = cardMap.getBankName();
			String bankCardNo = cardMap.getBankCardNo();
			String custName = cardMap.getCustName();
			loan.setReceiveAcct(bankCardNo);
			loan.setReceiveAcctName(custName);
			loan.setReceiveBankName(bankName);
			loan.setRepayAcct(bankCardNo);
			loan.setRepayAcctName(custName);
			loan.setRepayBankName(bankName);
			
			// 推送卡信息到新核心
			Map<String, String> reqMap = new HashMap<>();
			reqMap.put("loanId", loanId);
			reqMap.put("receiveBankName", bankName);
			reqMap.put("receiveAcctName", custName);
			reqMap.put("receiveAcctNo", bankCardNo);
			reqMap.put("paymentBankName", bankName);
			reqMap.put("paymentAcctName", custName);
			reqMap.put("paymentAcctNo", bankCardNo);
			MarvelsLogUtil.getInstance().info("reqMap=" + JSON.toJSONString(reqMap));
			JsonResult<Map> result = newMarvelCoreService.getForObjectPost(MarvelURLConstant.APPROVE_LOAN_API_MODIFYLOANAPPLYINFO, reqMap, Map.class);
			MarvelsLogUtil.getInstance().info("========" + JSON.toJSONString(result));
			
			com.marvels.approve.loan.dto.McamLoanApplyInfoDTO mcamLoanApplyInfoDTO = new com.marvels.approve.loan.dto.McamLoanApplyInfoDTO();
			mcamLoanApplyInfoDTO.setLoanId(loanId);
			mcamLoanApplyInfoDTO.setReceiveBankName(bankName);
			mcamLoanApplyInfoDTO.setReceiveAcctName(custName);
			mcamLoanApplyInfoDTO.setReceiveAcctNo(bankCardNo);
			mcamLoanApplyInfoDTO.setPaymentBankName(bankName);
			mcamLoanApplyInfoDTO.setPaymentAcctName(custName);
			mcamLoanApplyInfoDTO.setPaymentAcctNo(bankCardNo);
			int count = loanApplyProvideApi.modifyLoanApplyInfo(mcamLoanApplyInfoDTO);
			MarvelsLogUtil.getInstance().info("修改银行卡响应：========：" + count);
			
				

			//4 修改申请表银行卡相关信息
			loan.setLoanId(loanId);
			loanInfoIDragonService.input(loan);*/
			
			RealNameAuthentication realNameAuthentication = realNameAuthenticationService.queryByUserId(user.getId());
			String currentChannel  = ServerProperties.getValue(configPropertiesPath, "common.properties", "payment_channel");
			// 设置默认卡
			MarvelsPayResultDTO data = payService.updateDefaultCard(realNameAuthentication.getIdentityCardNo(), currentChannel, loanId, bankCardLoanDTO.getBankCardId());
			FintechResult jsonResult = new FintechResult();
			jsonResult.setData(data.getData());
			jsonResult.setErrorCode(data.getCode());
			jsonResult.setMessage(data.getMessage());

			if (!ErrorCode.SUCCESS.equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, data.getMessage());
			}
			
			if (LoanStatusConstant.LOAN_STATUS_K00.equals(acLoanApplyInfo.getStatus())) {
				loanInfoIDragonService.updateLoanStatus(loanId, LoanStatusConstant.LOAN_STATUS_K01);
			}
			
			// 扭转流程节点到绑卡完成
			AcLoanApplyNode applyNode = acLoanApplyNodeService.queryLastApplyNode(loanId);
			if (applyNode == null) {
				return FintechResult.getFailResult(ReturnCode.fail.getErrorCode(), "查询最新的节点信息为空");
			}
			// 当前节点为进件初始化节点时，才扭转节点状态到绑卡完成
			if (applyNode.getNodeCode() == ApplyNode.APPLY_INIT.getCode()) {
				acLoanApplyNodeService.insertSelective(loanId, ApplyNode.BIND_CARD);
			}
			
		   MarvelsLogUtil.getInstance().info("-----------bankCard/loan/{loanId}------e------------" + bankCardLoanDTO+ "--loanId = " + loanId);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("-----------bankCard/loan/{loanId}------error-------" + bankCardLoanDTO + "--loanId = " + loanId, e);
			return FintechResult.getResult(ErrorCode.FAIL, "系统异常", null);
		}
		return FintechResult.getResult(ErrorCode.SUCCESS, "操作成功", null);
	}
	
	/**
	 * 根据银行产品信息预绑卡接口发送验证码
	 * 
	 * @param bankcardSaveDTO
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public FintechResult bankPreBindCard(BankcardSaveDTO bankcardSaveDTO, String accessToken, String idFintechUmUser) {
		String loanId = null;
		try {
			User user = UserUtils.getUser(accessToken, idFintechUmUser);
			if (user == null) {
				return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
			}
			RealNameAuthentication realNameAuthentication = realNameAuthenticationService.queryByUserId(user.getId());
			if(realNameAuthentication == null) 	{return FintechResult.getResult(ErrorCode.EXCEPTION, "未实名", "");}
			
			
			//查询是否支持银行
			/*Map<String, String> reqMap = new HashMap<>();
			reqMap.put("bankCode", bankcardSaveDTO.getBankCode());
			reqMap.put("bankName", bankcardSaveDTO.getBankName());
			// 预绑卡调用新接口
			reqMap.put("bankCardNo", bankcardSaveDTO.getBankCardNum());
			reqMap.put("idno", realNameAuthentication.getIdentityCardNo());
			reqMap.put("userName", realNameAuthentication.getRealName());
			reqMap.put("phone", bankcardSaveDTO.getMobileNum());
			FintechResult jsonResult = newMarvelCoreService.getForObjectPost(MarvelURLConstant.FINANCIAL_PAY_API_PREBINDCARD, reqMap);*/
			
			Map<String, Object> reqMap = new HashMap<String, Object>();
			reqMap.put("bankCode", bankcardSaveDTO.getBankCode());
			reqMap.put("bankName", bankcardSaveDTO.getBankName());
			// 预绑卡调用新接口
			reqMap.put("bankCardNo", bankcardSaveDTO.getBankCardNum());
			reqMap.put("idno", realNameAuthentication.getIdentityCardNo());
			reqMap.put("userName", realNameAuthentication.getRealName());
			reqMap.put("phone", bankcardSaveDTO.getMobileNum());
			String paymentChannel  = ServerProperties.getValue(configPropertiesPath, "common.properties", "payment_channel"); 
			MarvelsPayResultDTO data = payService.preBind(reqMap, paymentChannel);
			FintechResult jsonResult = new FintechResult();
			jsonResult.setData(data.getData());
			jsonResult.setErrorCode(data.getCode());
			jsonResult.setMessage(data.getMessage());
			
			MarvelsLogUtil.getInstance().info("========bankPreBindCard:" + JSON.toJSONString(jsonResult));
			// 已经绑定的卡不能再绑定
			if ("-10000001".equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "您的卡[" + bankcardSaveDTO.getBankCardNum() + "]已绑定，不能重复绑定");
			}
			if (!ErrorCode.SUCCESS.equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "预绑卡异常");
			}
			Map<String, String> rtnMap = (Map<String, String>) jsonResult.getData();
			// 待验证状态
			if ("TO_VALIDATE".equals(rtnMap.get("returnStatus"))) {
				return FintechResult.getResult(ErrorCode.SUCCESS, "操作成功", rtnMap);
			}
			return FintechResult.getFailResult(ErrorCode.FAIL, "预绑卡失败");
			
		}catch(Exception e) {
			MarvelsLogUtil.getInstance().error("预绑卡异常=========================loanId" + loanId, e);
			return FintechResult.getResult(ErrorCode.EXCEPTION, "绑卡失败, 请换卡尝试", "");
		}
	}
	
	@Override
	public FintechResult sustainBankList(String accessToken,String idFintechUmUser) {
		User user = UserUtils.getUser(accessToken, idFintechUmUser);
		if (user == null) {
			return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
		}
		try {
			/*FintechResult bankResult = newMarvelCoreService.getForObjectPost(MarvelURLConstant.BANK_NAME_IS_SUSTAIN, null);*/
			
			McfeSustainBankListDTO record = new McfeSustainBankListDTO();
			List<McfeSustainBankListDTO> list = bankLimitRelevantApi.selectByBankName(record);
			FintechResult bankResult = new FintechResult();
			bankResult.setData(list);
			bankResult.setErrorCode(ErrorCode.SUCCESS);
			bankResult.setMessage("成功");
			
			if (!ErrorCode.SUCCESS.equals(bankResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "查询支持银行名称列表异常");
			}else{
				return FintechResult.getResult(ErrorCode.SUCCESS,"查询支持银行列表成功",(List<?>) bankResult.getData());
			}
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>>>查询支持银行名称列表异常",e);
			return FintechResult.getResult(ErrorCode.FAIL, "系统异常", null);
		}
		
	}
	
	@Override
	public FintechResult queryBnakCardBin(String bankCardNo) {
		MarvelsLogUtil.getInstance().info("========queryBnakCardBin========,bankCardNo=[" + bankCardNo + "]");
		
		
		/*Map<String, String> map = new HashMap<>();
		map.put("bankCardNo", bankCardNo);
		FintechResult jsonResult = newMarvelCoreService.getForObjectPost(MarvelURLConstant.FINANCIAL_PAY_API_QUERY_CARD_BIN, map);*/
		
		if(StringUtils.isEmpty(bankCardNo)){
			return FintechResult.getFailNullParam("绑定卡信息入参为空");
		}
		String paymentChannel  = ServerProperties.getValue(configPropertiesPath, "common.properties", "payment_channel");
		MarvelsPayResultDTO data = payService.getBankCardInfo(bankCardNo,paymentChannel);
		FintechResult jsonResult = new FintechResult();
		jsonResult.setMessage(data.getMessage());
		jsonResult.setErrorCode(data.getCode());
		jsonResult.setData(data.getData());
		
		MarvelsLogUtil.getInstance().info("========queryBnakCardBin:" + JSON.toJSONString(jsonResult));
		if (!ErrorCode.SUCCESS.equals(jsonResult.getErrorCode())) {
			return FintechResult.getFailResult(ErrorCode.FAIL, "获取卡BIN信息异常");
		}
		return FintechResult.getSuccessResult(jsonResult.getData());
	}
	
	
	/**
	 * 查询绑定成功的银行卡列表 
	 * 优先展示当前渠道的银行卡，如果存在老渠道的也会查出，根据is_support字段区分当前渠道是否支持
	 */
	@Override
	public FintechResult queryBankList(String accessToken, String idFintechUmUser) {
		try {
			//1 获取用户信息以及实名认证信息
			User user = UserUtils.getUser(accessToken, idFintechUmUser);
			if (user == null) {
				return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
			}
			ResultData<Boolean> selectByUserIdResponse = realNameAuthenticationService.selectByUserId(user.getId());
			if (!selectByUserIdResponse.getData()) {
				return FintechResult.getResult(ErrorCode.NO_VERIFY, "", null);
			}
			RealNameAuthentication realNameAuthentication = realNameAuthenticationService.queryByUserId(user.getId());

			String currentChannel  = ServerProperties.getValue(configPropertiesPath, "common.properties", "payment_channel");
			MarvelsPayResultDTO data = payService.queryBankList(realNameAuthentication.getIdentityCardNo(), currentChannel);
			FintechResult jsonResult = new FintechResult();
			jsonResult.setData(data.getData());
			jsonResult.setErrorCode(data.getCode());
			jsonResult.setMessage(data.getMessage());
			
			MarvelsLogUtil.getInstance().info("========queryBankList:" + JSON.toJSONString(jsonResult));
			if (!ErrorCode.SUCCESS.equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "获取银行卡列表失败");
			}
			List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
			List<McfePaymentUserBindCard> rtnMap =  (List<McfePaymentUserBindCard>) jsonResult.getData();
			for (McfePaymentUserBindCard card : rtnMap) {
				Map<String, Object> bankMap = new HashMap<>();
				bankMap.put("id", card.getId());
				bankMap.put("bankCode", card.getBankCode());
				bankMap.put("bankName", card.getBankName());
				bankMap.put("mobileNum", card.getPhome());
				bankMap.put("bankCardNo", card.getBankCardNo());
				bankMap.put("defaultCard", card.getDefaultCard());
				bankMap.put("isSupport", card.getIsSupport());
				bankMap.put("bankCardType", "借记卡");
				returnList.add(bankMap);
			}
			return FintechResult.getResult(ErrorCode.SUCCESS, "操作成功", returnList);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("=====queryBankList=====", e);
			return FintechResult.getResult(ErrorCode.FAIL, "系统异常", null);
		}
	}


	/**
	 * 设置默认卡
	 */
	@Override
	public FintechResult setDefaultCard(String accessToken, String idFintechUmUser, BankCardLoanDTO bankCardLoanDTO) {
		//1 获取用户信息以及实名认证信息
		try {
			User user = UserUtils.getUser(accessToken, idFintechUmUser);
			if (user == null) {
				return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
			}
			ResultData<Boolean> selectByUserIdResponse = realNameAuthenticationService.selectByUserId(user.getId());
			if (!selectByUserIdResponse.getData()) {
				return FintechResult.getResult(ErrorCode.NO_VERIFY, "", null);
			}
			RealNameAuthentication realNameAuthentication = realNameAuthenticationService.queryByUserId(user.getId());
			String currentChannel  = ServerProperties.getValue(configPropertiesPath, "common.properties", "payment_channel");
			// 查询在途的loanId
			String loanId = loanInfoIDragonService.selectCustApplyingLoanId(realNameAuthentication.getIdentityCardNo());
			// 设置默认卡
			MarvelsPayResultDTO data = payService.updateDefaultCard(realNameAuthentication.getIdentityCardNo(), currentChannel, loanId, bankCardLoanDTO.getBankCardId());
			FintechResult jsonResult = new FintechResult();
			jsonResult.setData(data.getData());
			jsonResult.setErrorCode(data.getCode());
			jsonResult.setMessage(data.getMessage());
			return jsonResult;
		}catch(MarvelsBusinessException e){
			MarvelsLogUtil.getInstance().error(">>>>>>>>>设置默认卡失败", e);
			return FintechResult.getResult(ErrorCode.FAIL, e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error("=====设置默认卡异常=====", e);
			return FintechResult.getResult(ErrorCode.EXCEPTION, "设置默认卡异常", null);
		}
		
	}
}
